import {
	createOrderApi
} from "./api.js";

function _createOrder(goodsId, couponId, type, userId) {
	console.log('_createOrder===', type);
	switch (type) {
		case 'wxpay':
			return createOrderApi(goodsId, couponId, 0, userId);
		case 'alipay':
			return createOrderApi(goodsId, couponId, 1, userId);
		case 'appleiap':
			return createOrderApi(goodsId, couponId, 2, userId);
		default:
			return Promise.reject(new Error('无效的支付方式'))
	}
}

function _provider(type = '', order, productIds = []) {
	// #ifdef H5
	return Promise.reject(new Error('H5平台暂不支持支付功能'));
	// #endif
	// #ifndef H5
	return new Promise((resolve, reject) => {
		uni.getProvider({
			service: 'payment',
			success(res) {
				if (type === 'appleiap') {
					let iapChannel = res.providers.find((channel) => {
						return channel.id === type;
					});
					if (iapChannel) {
						let orderInfo = {
							productid: order.bindId,
							username: order.orderId,
							quantity: 1,
							manualFinishTransaction: true,
							optimize: true
						};
						console.log('orderInfo', orderInfo, productIds);
						iapChannel.requestOrder(productIds, res => {
							resolve({
								provider: type,
								orderInfo: orderInfo,
							});
						}, e => {
							reject(e);
						});
					} else {
						reject(new Error('暂不支持iap支付'));
					}
				} else if (~res.provider.indexOf(type)) {
					resolve({
						provider: type,
						orderInfo: order,
					});
				} else {
					console.error('error provider===>', res.provider, type);
					reject(new Error('暂不支持此类型支付'));
				}
			},
			fail(err) {
				reject(err);
			}
		});
	});
	// #endif
}

function _payment(data) {
	return new Promise((resolve, reject) => {
		// #ifdef MP-DINGTALK
		dd.pay({
			info: data.orderInfo,
			success(res) {
				if (parseInt(res.resultStatus) != 9000) {
					reject(res.resultCode);
				} else {
					let result = JSON.parse(res.result);
					let orderId = result.alipay_trade_app_pay_response.out_trade_no
					resolve({
						provider: 'alipay',
						orderInfo: orderId,
						response: res,
					});
				}
			},
			fail(err) {
				reject(err);
			}
		});
		return false;
		// #endif
		let options;
		// #ifdef MP-ALIPAY
		options = {
			tradeNO: data.orderInfo.orderId,
			success(res) {
				console.error('===_payment==callback===suc', res);
				if (parseInt(res.resultCode) != 9000) {
					reject(res.resultCode);
				} else {
					resolve({
						provider: 'alipay',
						orderInfo: data.orderInfo.orderId,
						response: res,
					});
				}
			},
			fail(err) {
				console.error('===_payment==callback===fail', err);
				reject(err);
			}
		}
		// #endif
		// #ifdef MP-WEIXIN
		options = {
			provider: data.provider,
			orderInfo: data.orderInfo.order,
			success(res) {
				resolve({
					provider: data.provider,
					orderInfo: data.orderInfo.order,
					response: res,
				});
			},
			fail(err) {
				reject(err);
			}
		}
		if (data.orderInfo['jsApiParameters']) {
			let jsApiParameters = JSON.parse(data.orderInfo['jsApiParameters']);
			Object.assign(options, jsApiParameters);
		}
		// #endif
		// #ifndef MP
		options = {
			provider: data.provider,
			orderInfo: data.orderInfo,
			success(res) {
				resolve({
					provider: data.provider,
					orderInfo: data.orderInfo,
					response: res,
				});
			},
			fail(err) {
				reject(err);
			}
		}
		// #endif
		uni.requestPayment(options);
	});
}

function payment(goodsId, couponId, type, productIds, userId) {
	return _createOrder(goodsId, couponId, type, userId)
		.then(order => _provider(type, order, productIds))
		.then(res => _payment(res))
}

export default payment;